package com.client.controller;

import com.client.pojo.LikeNotifications;
import com.client.service.LikeNotificationsService;
import com.client.utils.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

/**
 * <p>
 * 用户点赞通知表 前端控制器
 * </p>
 *
 * @author ckick
 * @since 2025-09-20 08:28:54
 */
@RestController
@RequestMapping("/likeNotifications")
public class LikeNotificationsController {
    @Autowired
    private LikeNotificationsService likeNotificationsService;
    @RequestMapping("getLikeNotifications/{receiverId}")
    public ResponseMsg getLikeNotifications(@PathVariable Integer receiverId) {
        List<LikeNotifications> likeNotifications = likeNotificationsService.getByIds(receiverId);
        if (likeNotifications == null) {
            return ResponseMsg.getInstance(ResponseMsg.ERROR,"暂无点赞通知的消息~");
        }
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS,"成功获取点赞通知的消息~",likeNotifications);
    }

//点赞词条增加通知
    @PostMapping("addEntryNotification")
    public String addEntryNotification(@RequestBody Map<String,Object> param) {
        //被点赞者id
        Integer receiverId = (Integer) param.get("authorId");
        //点赞者id
        Integer likerId = (Integer) param.get("userId");
        //点赞者名字
        String likedName = (String) param.get("username");
        //点赞的词条名
        String entryName = (String) param.get("entryName");
        //点赞标题
        String title = likedName+"点赞了你的词条"+entryName;
        //点赞的详细内容
        String content = likedName+"点赞了你的词条"+entryName+"\n"+"请及时查看";

        LikeNotifications likeNotifications = new LikeNotifications();
        likeNotifications.setReceiverId(receiverId);
        likeNotifications.setLikerId(likerId);
        likeNotifications.setTitle(title);
        likeNotifications.setContent(content);

        likeNotificationsService.save(likeNotifications);
        return "success";
    }
    
//取消点赞删除通知
    @PostMapping("deleteEntryNotification")
    public String deleteEntryNotification(@RequestBody Map<String,Object> param) {
        //被点赞者id
        Integer receiverId = (Integer) param.get("authorId");
        //点赞者id
        Integer likerId = (Integer) param.get("userId");
        //点赞者名字
        String likedName = (String) param.get("username");
        //点赞的词条名
        String entryName = (String) param.get("entryName");
        //点赞标题
        String title = likedName+"点赞了你的词条"+entryName;
        //删除通知
        likeNotificationsService.remove(new LambdaQueryWrapper<LikeNotifications>()
                .eq(LikeNotifications::getReceiverId, receiverId)
                .eq(LikeNotifications::getLikerId, likerId)
                .eq(LikeNotifications::getTitle, title));
        return "success";
    }
}
